function Map3D(earth, options) {
    this.version = "build20211111";

    this.earth = earth;
    this.options = options;

    this.earth.interaction.picking.enabled = true;

    this.buildingListCache = {
        Set: function (key, value) {
            this[key] = value
        },
        Get: function (key) {
            return this[key]
        },
        Contains: function (key) {
            return this.Get(key) == null ? false : true
        },
        Remove: function (key) {
            delete this[key]
        }
    };

    this.buildingIdList = [];

    this.currentShowBuilding = {
        "buildingId": "",
        "floorIds": []
    };

    this.showAllFloor = true;
}

Map3D.prototype._parseMapDataUrl = function (mapData) {
    var dataUrl = mapData.dataUrl;
    var modelData = mapData.modelData;
    for (var index = 0; index < modelData.length; index++) {
        var element = modelData[index];

        if (element.czmObject && element.czmObject.xbsjType == "Tileset") {
            element.czmObject.url = dataUrl + element.czmObject.url;
        }
    }


    if (mapData.buildings) {
        var buildings = mapData.buildings;
        for (var index = 0; index < buildings.length; index++) {
            var building = buildings[index];

            var buildingFloorListCache = {
                Set: function (key, value) {
                    this[key] = value
                },
                Get: function (key) {
                    return this[key]
                },
                Contains: function (key) {
                    return this.Get(key) == null ? false : true
                },
                Remove: function (key) {
                    delete this[key]
                }
            };


            if (building.modelType == "floors" && building.floors) {
                var floors = building.floors;
                for (var i = 0; i < floors.length; i++) {
                    var floor = floors[i];
                    buildingFloorListCache.Set(floor.id, floor);
                    if (floor.model && floor.model.czmObject && floor.model.czmObject.url) {
                        floor.model.czmObject.url = dataUrl + floor.model.czmObject.url;
                    }
                }
            }
            building.buildingFloorListCache = buildingFloorListCache;
        }

    }

    this.initialView = mapData.initialView;




}

Map3D.prototype.setTilesetReadyPromiseEvent = function (event) {
    this.TilesetReadyPromiseEvent = event;
}

Map3D.prototype.openMap = function (mapData) {

    this._parseMapDataUrl(mapData);



    var floorControlOption = mapData.floorControlOption;


    var buildings = mapData.buildings;
    // var floors = mapData.floors;



    var imageryData = mapData.imageryData;
    for (var index = 0; index < imageryData.length; index++) {
        var element = imageryData[index];

        this.earth.sceneTree.root.children.push(element);

    }

    if (mapData.cameraViewManager) {
        this.earth.cameraViewManager = mapData.cameraViewManager;
    }

    var modelData = mapData.modelData;

    for (var index = 0; index < modelData.length; index++) {
        var element = modelData[index];

        this.earth.sceneTree.root.children.push(element);


    }


    if (buildings && buildings.length != 0) {
        for (var index = 0; index < buildings.length; index++) {
            var building = buildings[index];

            var buildingId = building.id;

            //加入全局变量 缓存
            this.buildingListCache.Set(buildingId, building);
            this.buildingIdList.push(buildingId);

            var buildingPoiRef = building.poiRef;

            var buildingPoi = building.poi;



            var buildingModelType = building.modelType;
            if (buildingModelType == "building") {
                // var buildingModelRef = building.buildingModelRef;

                //添加建筑物图标
                this.earth.sceneTree.root.children.push(buildingPoi);

                var buildingPoiObject = this.earth.sceneTree.$refs[buildingPoiRef].czmObject;

                var that = this;
                //设置建筑物点击事件
                buildingPoiObject.onclick = function (e) {
                    var _name = e.id._name;

                    var _building_id = _name.split("_")[0];

                    if (!_name && !_building_id) {
                        return;
                    }


                    if (_building_id == that.currentShowBuilding.buildingId) {//如果点击的建筑物与当前建筑物相同，直接返回
                        return;
                    } else {

                        this._clearFloorControl();

                        var flattenPolygon = that.earth.sceneTree.$refs[_name].flattenPolygon;

                        that.earth.sceneTree.$refs['flatten_tileset'].czmObject.polygons = [];

                        that.earth.sceneTree.$refs['flatten_tileset'].czmObject.polygons.push(
                            flattenPolygon
                        )

                        var _floorIds = that.currentShowBuilding.floorIds;
                        for (var k = 0; k < _floorIds.length; k++) {
                            var _floorId = _floorIds[k];
                            if (that.earth.sceneTree.$refs[_floorId]) {
                                var floorObject = that.earth.sceneTree.$refs[_floorId].czmObject;
                                if (floorObject) {
                                    floorObject.show = false;
                                }
                            }
                        }

                    }



                    that.currentShowBuilding = {
                        "buildingId": _building_id,
                        "floorIds": []
                    }

                };
                //鼠标移动到图标上，放大
                buildingPoiObject.onmouseover = function () {
                    buildingPoiObject.scale = 1.1;
                }
                //鼠标移出图标，还原
                buildingPoiObject.onmouseout = function () {
                    buildingPoiObject.scale = 1.0;
                }






            } else if (buildingModelType == "floors") {

                var buildingFloors = building.floors;

                for (var i = 0; i < buildingFloors.length; i++) {
                    var buildingFloor = buildingFloors[i];

                    var buildingFloorId = buildingFloor.id;

                    if (buildingFloor.model) {
                        this.earth.sceneTree.root.children.push(buildingFloor.model);
                    }

                    if (this.earth.sceneTree.$refs[buildingFloorId]) {
                        if (buildingFloor.modelType == "vector") {
                            var floorObject = this.earth.sceneTree.$refs[buildingFloorId].czmObject;

                            if (floorObject) {
                                floorObject.xbsjStyle = JSON.stringify(buildingStyle);
                            }
                        }

                    }
                }

                //添加建筑物图标
                this.earth.sceneTree.root.children.push(buildingPoi);
                var buildingPoiObject = this.earth.sceneTree.$refs[buildingPoiRef].czmObject;
                var that = this;
                //设置建筑物点击事件
                buildingPoiObject.onclick = function (e) {
                    var _name = e.id._name;

                    var _building_id = _name.split("_")[0];

                    if (!_name && !_building_id) {
                        return;
                    }

                    if (_building_id == that.currentShowBuilding.buildingId) {//如果点击的建筑物与当前建筑物相同，直接返回
                        return;
                    } else {


                        //先还原其他建筑物
                        that.earth.sceneTree.$refs['flatten_tileset'].czmObject.polygons = [];

                        var _floorIds = that.currentShowBuilding.floorIds;
                        for (var k = 0; k < _floorIds.length; k++) {
                            var _floorId = _floorIds[k];
                            if (that.earth.sceneTree.$refs[_floorId]) {
                                var floorObject = that.earth.sceneTree.$refs[_floorId].czmObject;
                                if (floorObject) {
                                    floorObject.show = false;
                                }
                            }
                        }



                        //把点击对象的建筑物压平对象塞入地图。使点击对象建筑物压平
                        var flattenPolygon = that.earth.sceneTree.$refs[_name].flattenPolygon;

                        that.earth.sceneTree.$refs['flatten_tileset'].czmObject.polygons.push(
                            flattenPolygon
                        )

                        var newBuilding = that.buildingListCache.Get(_building_id);

                        var newFloors = newBuilding.floors;
                        var newFloorsIds = [];
                        for (var j = 0; j < newFloors.length; j++) {
                            var newFloor = newFloors[j];

                            var newFloorId = newFloor.id;
                            newFloorsIds.push(newFloorId);

                            if (that.earth.sceneTree.$refs[newFloorId]) {
                                var newFloorObject = that.earth.sceneTree.$refs[newFloorId].czmObject;

                                if (newFloorObject) {
                                    newFloorObject.show = true;
                                }
                            }
                        }

                        that.currentShowBuilding = {
                            "buildingId": _building_id,
                            "floorIds": newFloorsIds
                        }

                        if (that.BulidingOpenedEvent) {
                            that.BulidingOpenedEvent(that.currentShowBuilding);
                        }


                        that._initFloorControl(floorControlOption, newBuilding)
                    }

                }

            };
            //鼠标移动到图标上，放大
            buildingPoiObject.onmouseover = function () {
                buildingPoiObject.scale = 1.1;
            }
            //鼠标移出图标，还原
            buildingPoiObject.onmouseout = function () {
                buildingPoiObject.scale = 1.0;
            }


        }
    }










    if (this.earth.sceneTree.$refs['base_tileset'] && this.earth.sceneTree.$refs['base_tileset'].czmObject) {
        if (!mapData.cameraViewManager) {
            this.earth.sceneTree.$refs['base_tileset'].czmObject.flyTo();
        }

        //纠正模型高度
        // tileset.root.children[0].transform[14] = 0;

        // this.earth.sceneTree.$refs['base_tileset'].czmObject.flyTo();

        // if (mapData.initialView) {

        //     var initialView = mapData.initialView;
        //     this.earth.czm.viewer.camera.setView({

        //         destination: Cesium.Cartesian3.fromDegrees(initialView.destination.longitude, initialView.destination.latitude, initialView.destination.altitude),
        //         orientation: {
        //             heading: Cesium.Math.toRadians(initialView.orientation.heading),
        //             pitch: Cesium.Math.toRadians(initialView.orientation.pitch),
        //             roll: Cesium.Math.toRadians(initialView.orientation.roll)
        //         }



        //     });
        // } else {
        //     this.earth.sceneTree.$refs['base_tileset'].czmObject.flyTo();
        // }

        if (this.TilesetReadyPromiseEvent) {
            if (this.earth.sceneTree.$refs['base_tileset'].czmObject._tileset) {
                this.earth.sceneTree.$refs['base_tileset'].czmObject._tileset.readyPromise.then(
                    this.TilesetReadyPromiseEvent
                )
            }

        }

    }


    // 1.2.4 数据双向绑定
    // disposers 用来收集资源销毁函数，并在析构时自动调用！
    // this._disposers = [];

    // 当url发生变动时，自动飞入3dtiles数据处
    // this._disposers.push(XE.MVVM.watch(base_tileset, 'ready', ready => (ready && base_tileset.flyTo())));
}


Map3D.prototype._initFloorControl = function (floorControlOption, building) {

    if (floorControlOption.floorControlVisible) {


        var floorControl = document.getElementsByClassName("floor-control")[0];
        var nodesLenght = floorControl.childNodes.length;
        for (var index = 0; index < nodesLenght; index++) {
            floorControl.childNodes[0].remove();
        }


        if (floorControlOption.showAllFloorButton) {
            var allFloorButtonElement = document.createElement("img");
            allFloorButtonElement.style = "margin-left: 5px;margin-top: 5px;width:22px;height:22px;cursor: pointer; ";
            allFloorButtonElement.src = "";


            var that = this;

            allFloorButtonElement.addEventListener("click", function (event) {
                var srcElement = event.srcElement;
                that.FloorChangedEvent("allFloor");
                that.showAllFloor = !that.showAllFloor;

                if (that.showAllFloor) {
                    srcElement.src = "";

                    var _floorIds = that.currentShowBuilding.floorIds;
                    for (var k = 0; k < _floorIds.length; k++) {
                        var _floorId = _floorIds[k];
                        if (that.earth.sceneTree.$refs[_floorId]) {
                            var floorObject = that.earth.sceneTree.$refs[_floorId].czmObject;
                            if (floorObject) {
                                floorObject.show = true;
                            }
                        }
                    }


                } else {
                    srcElement.src = "";

                    var _floorIds = that.currentShowBuilding.floorIds;

                    var activeElement = event.srcElement.parentElement.getElementsByClassName("active")[0];

                    var active_floor_id = activeElement.getAttribute("floorId");
                    for (var k = 0; k < _floorIds.length; k++) {
                        var _floorId = _floorIds[k];

                        if (_floorId != active_floor_id) {
                            if (that.earth.sceneTree.$refs[_floorId]) {
                                var floorObject = that.earth.sceneTree.$refs[_floorId].czmObject;
                                if (floorObject) {
                                    floorObject.show = false;
                                }
                            }
                        } else {
                            if (that.earth.sceneTree.$refs[_floorId]) {
                                var floorObject = that.earth.sceneTree.$refs[_floorId].czmObject;
                                if (floorObject) {
                                    floorObject.show = true;
                                }
                            }
                        }
                    }

                }



                // that.mapbox3d._showLayerByFloor();

            });

            floorControl.appendChild(allFloorButtonElement);

        }
        var floors = building.floors;

        for (var index = 0; index < floors.length; index++) {
            var floor = floors[index];

            var that = this;
            var floorElement = document.createElement("button");
            floorElement.innerText = floor.name;
            floorElement.setAttribute("floorId", floor.id);


            floorControl.appendChild(floorElement);

            floorElement.addEventListener("click", function (event) {
                var srcElement = event.srcElement;
                var elms = event.srcElement.parentElement.getElementsByClassName("active")
                while (elms[0]) {
                    elms[0].classList.remove("active");
                }
                srcElement.classList.add("active");



                if (!that.showAllFloor) {
                    // that.currentShowBuilding;

                    var active_floor_id = srcElement.getAttribute("floorId");
                    that.currentFloor = active_floor_id;
                    var currentBuilding = that.currentShowBuilding;

                    var currentBuildingFloor = {
                        "buildingId": currentBuilding.buildingId,
                        "floorId": active_floor_id
                    }
                    //当前切换的建筑和楼层
                    that.FloorChangedEvent(currentBuildingFloor);

                    var _floorIds = that.currentShowBuilding.floorIds;
                    for (var k = 0; k < _floorIds.length; k++) {
                        var _floorId = _floorIds[k];
                        if (_floorId != active_floor_id) {
                            if (that.earth.sceneTree.$refs[_floorId]) {
                                var floorObject = that.earth.sceneTree.$refs[_floorId].czmObject;
                                if (floorObject) {
                                    floorObject.show = false;
                                }
                            }
                        } else {
                            if (that.earth.sceneTree.$refs[_floorId]) {
                                var floorObject = that.earth.sceneTree.$refs[_floorId].czmObject;
                                if (floorObject) {
                                    floorObject.show = true;
                                }
                            }
                        }



                    }


                }
            })

        }
    }

}

Map3D.prototype._clearFloorControl = function () {
    this.showAllFloor = true;

    var floorControl = document.getElementsByClassName("floor-control")[0];
    var nodesLenght = floorControl.childNodes.length;
    for (var index = 0; index < nodesLenght; index++) {
        floorControl.childNodes[0].remove();
    }


}

var buildingStyle = {
    color: {
        conditions: [
            ['${typeId} === "100000"', 'color("#456fcb")'],
            ['${typeId} === "100100"', 'color("#69cbe0")'],
            ['${typeId} === "100200"', 'color("#419bd3")'],
            ['${typeId} === "100300"', 'color("#cea474")'],
            ['${typeId} === "170100"', 'color("#4fc0cf")'],
            ['${typeId} === "170200"', 'color("#4fc0cf")'],
            ['${typeId} === "190700"', 'color("#4fc0cf")'],
            ['${typeId} === "190100"', 'color("#4fc0cf")'],

            ['${typeId} === "200000"', 'color("#4fb079")'],
            ['${typeId} === "200001"', 'color("#8abc24")'],

            ['${typeId} === "200002"', 'color("#438adb")'],
            ['${typeId} === "200003"', 'color("#82b6c0")'],
            ['${typeId} === "200004"', 'color("#0d87d0")'],
            ['${typeId} === "200011"', 'color("#6cbccc")'],
            ['${typeId} === "200012"', 'color("#e1daa3")'],


            ['${typeId} === "200005"', 'color("#6cbccc")'],
            ['${typeId} === "200007"', 'color("#1e5c85")'],
            ['${typeId} === "200008"', 'color("#2b9fbf")'],

            ['${typeId} === "200100"', 'color("#83d5d9")'],
            ['${typeId} === "200102"', 'color("#b6b6b6")'],


            ['${typeId} === "200103"', 'color("#59aacf")'],
            ['${typeId} === "200104"', 'color("#59a9cf")'],
            ['${typeId} === "200105"', 'color("#59aacf")'],
            ['${typeId} === "200106"', 'color("#b3d863")'],
            ['${typeId} === "200107"', 'color("#e1a580")'],
            ['${typeId} === "200108"', 'color("#e1a780")'],
            ['${typeId} === "200109"', 'color("#59aacf")'],
            ['${typeId} === "200110"', 'color("#e1e1e1")'],
            ['${typeId} === "200111"', 'color("#68a45c")'],
            ['${typeId} === "200112"', 'color("#68a35b")'],
            ['${typeId} === "200113"', 'color("#cfcfcf")'],
            ['${typeId} === "200114"', 'color("#35a4d0")'],
            ['${typeId} === "200200"', 'color("#3db2a5")'],

            ['${typeId} === "200204"', 'color("#739b2d")'],
            ['${typeId} === "200205"', 'color("#80e1a2")'],
            ['${typeId} === "200207"', 'color("#80e1a2")'],
            ['${typeId} === "200209"', 'color("#80e0a1")'],


            ['${typeId} === "200401"', 'color("#c0d1d8")'],
            ['${typeId} === "200501"', 'color("#6da398")'],
            ['${typeId} === "200502"', 'color("#d09090")'],
            ['${typeId} === "200503"', 'color("#6da398")'],

            ['${typeId} === "210100"', 'color("#d0c991")'],
            ['${typeId} === "210101"', 'color("#c3dba4")'],
            ['${typeId} === "210102"', 'color("#bfdeda")'],
            ['${typeId} === "210103"', 'color("#bfdeda")'],

            ['${typeId} === "210200"', 'color("#579bd6")'],
            ['${typeId} === "210300"', 'color("#689ec2")'],
            ['${typeId} === "210400"', 'color("#c7dd55")'],
            ['${typeId} === "210401"', 'color("#c7dd55")'],
            ['${typeId} === "210402"', 'color("#7aa2bd")'],


            ['${typeId} === "210403"', 'color("#70babe")'],
            ['${typeId} === "210501"', 'color("#89c078")'],
            ['${typeId} === "210502"', 'color("#c7d2d6")'],

            ['${typeId} === "210600"', 'color("#d49d63")'],


            ['${typeId} === "260100"', 'color("#ced0af")'],
            ['${typeId} === "260101"', 'color("#6399cb")'],
            ['${typeId} === "260102"', 'color("#968ec4")'],
            ['${typeId} === "260400"', 'color("#a5a772")'],
            ['${typeId} === "260500"', 'color("#7ba646")'],


            ['${typeId} === "280100"', 'color("#67b3a0")'],

            ['${typeId} === "280102"', 'color("#67b3a0")'],
            ['${typeId} === "280103"', 'color("#67b3a0")'],
            ['${typeId} === "280105"', 'color("#ab4343")'],
            ['${typeId} === "280107"', 'color("#ab4343")'],
            ['${typeId} === "280108"', 'color("#d49d63")'],
            ['${typeId} === "280109"', 'color("#ab4343")'],
            ['${typeId} === "280110"', 'color("#cfcfcf")'],

            ['${typeId} === "280200"', 'color("#cfcfcf")'],
            ['${typeId} === "280201"', 'color("#1A9BFF")'],
            ['${typeId} === "280202"', 'color("#98c3d2")'],


            ['${typeId} === "280300"', 'color("#cfcfcf")'],
            ['${typeId} === "280401"', 'color("#c0d1d8")'],
            ['${typeId} === "280501"', 'color("#6da398")'],
            ['${typeId} === "280502"', 'color("#d09090")'],

            ['${typeId} === "280600"', 'color("#cfcfcf")'],
            ['${typeId} === "280700"', 'color("#d09090")'],

            ['${typeId} === "280704"', 'color("#cfcfcf")'],
            ['${typeId} === "280705"', 'color("#cfcfcf")'],

            ['${typeId} === "300000"', 'color("#d9d9db")'],
            ['${typeId} === "300001"', 'color("#8ea7ba")'],
            ['${typeId} === "500000"', 'color("#89c078")'],
            ['${typeId} === "600001"', 'color("#cfcfcf")'],
            ['${typeId} === "600002"', 'color("#e1a580")'],
            ['${typeId} === "600003"', 'color("#83d5d9")'],
            ['${typeId} === "600004"', 'color("#59a9cf")'],
            ['${typeId} === "600005"', 'color("#b3d863")'],
            ['${typeId} === "600006"', 'color("#70babe")'],

            ['true', 'color("#c0c0c0")']
        ]
    }


};

Map3D.prototype.getBuildingByPosition = function (position) {

    var _buildingId = '';
    var pt = turf.point(position);
    if (!pt) {
        return _floorId;
    }
    for (var index = 0; index < this.buildingIdList.length; index++) {
        var buildingId = this.buildingIdList[index];
        // console.log(buildingId);
        var building = this.buildingListCache.Get(buildingId);
        var buildingPolygon = building.buildingPolygon;
        if (buildingPolygon) {
            var poly = turf.polygon(buildingPolygon);
            //    console.log(poly);
            if (poly) {

                var isIn = turf.booleanPointInPolygon(pt, poly);
                if (isIn) {
                    _buildingId = buildingId
                    break;
                }
            }
        }
    }

    return _buildingId;

}
Map3D.prototype.getBuildingByBuildingId = function (buildingId) {
    var building = this.buildingListCache.Get(buildingId);
    return building;
}

Map3D.prototype.getBuildingFloor = function (buildingId, floorId) {
    var building = this.buildingListCache.Get(buildingId);
    if (building) {
        var floor = building.buildingFloorListCache.Get(floorId);
        return floor;
    }
}



Map3D.prototype.getCurrentShowBuilding = function () {
    return this.currentShowBuilding;
}

Map3D.prototype.moveMarkerToInBuilding = function (viewer, entity, toPosition, options) {

    var buildingId;
    var floorId;

    if (options.buildingId) {
        buildingId = options.buildingId;
    }
    if (options.floorId) {
        floorId = options.floorId;
    }

    var building = this.buildingListCache.Get(buildingId);
    // var floors

}

Map3D.prototype.moveMarkerTo = function (viewer, entity, toPosition, options) {
    var ellipsoid = viewer.scene.globe.ellipsoid;
    var cartographic = ellipsoid.cartesianToCartographic(entity.position._value);
    var lat = Cesium.Math.toDegrees(cartographic.latitude);
    var lng = Cesium.Math.toDegrees(cartographic.longitude);

    var height = cartographic.height;

    var fromPosition = [lng, lat];

    var toPosition = toPosition;
    var nstep = 10;
    var duration = 1000;//毫秒
    if (options.duration) {
        duration = options.duration * 1000;
    }

    var route = _getMoveToRoute(fromPosition, toPosition, nstep);

    var timeInterval = Math.floor(duration / nstep);//毫秒


    var animateTrack = new AnimateTrack(entity, route, timeInterval, {
        'height': height
    });

    animateTrack.animateFrameMove();


}




Map3D.prototype.setFloorChangedEvent = function (event) {
    this.FloorChangedEvent = event;
}

Map3D.prototype.setBulidingOpenedEvent = function (event) {
    this.BulidingOpenedEvent = event;
}


function AnimateTrack(entity, route, timeInterval, options) {
    this.entity = entity;
    // this.marker = marker;
    this.route = route;
    this.counter = 0;
    this.intervalId;
    if (timeInterval) {
        this.timeInterval = timeInterval;
    } else {
        this.timeInterval = 1000;
    }

    this.totalStep = route.features[0].geometry.coordinates.length;
    this.options = options;

    var height = 5;
    if (options.height) {
        height = options.height;
    }
    // this.fps = 60;

    // this.interval = this.duration / this.fps;
    this.then = Date.now();

    var that = this;

    this.delta;
    this.render = function () {

        if (that.counter < that.totalStep - 1) {
            that.frameId = requestAnimationFrame(that.render);
            var now = Date.now();
            that.delta = now - that.then;
            if (that.delta > that.timeInterval) {

                that.then = now - (that.delta % that.timeInterval);
                that.move();
            }
        } else {
            cancelAnimationFrame(that.frameId);
        }


    }

    this.move = function () {

        var _coord = that.route.features[0].geometry.coordinates[that.counter];
        var coord = turf.getCoord(_coord);
        if (coord) {
            // that.marker.setLngLat(coord);

            // var _height =height;
            entity.position = Cesium.Cartesian3.fromDegrees(coord[0], coord[1], height);

            that.counter += 1;
        }


    }

};




AnimateTrack.prototype.animateFrameMove = function () {
    var that = this;
    this.frameId = requestAnimationFrame(that.render);
}


AnimateTrack.prototype.stopFrameMove = function () {
    var that = this;
    if (that.frameId) {
        cancelAnimationFrame(that.frameId);
    }
}

function _getMoveToRoute(fromPosition, toPosition, nstep) {
    var route = {
        "type": "FeatureCollection",
        "features": [{
            "type": "Feature",
            "geometry": {
                "type": "LineString",
                "coordinates": []
            }
        }]
    };

    // var _fromPoint;
    var fromPoint = turf.point(fromPosition);
    var toPoint = turf.point(toPosition);

    var tempLine = turf.lineString([fromPosition, toPosition], { name: "tempLine" });

    var distance = turf.distance(fromPoint, toPoint, { units: 'kilometers' });
    var splitLength = distance / nstep;
    var leftLength = distance - nstep * splitLength;

    route.features[0].geometry.coordinates.push(fromPoint);
    route.features[0].distance = distance;

    for (var i = 1; i < nstep; i++) {
        var nlength = i * splitLength;
        var png = turf.along(tempLine, nlength);
        route.features[0].geometry.coordinates.push(png.geometry.coordinates);
    }
    if (splitLength > 0) {
        route.features[0].geometry.coordinates.push(toPoint.geometry.coordinates);
    }

    // console.log(route);

    return route;
}


function translate(trans, position) {//局部坐标转全局坐标
    let rotate = Cesium.Math.toRadians(0); //转成弧度
    let quat = Cesium.Quaternion.fromAxisAngle(
        Cesium.Cartesian3.UNIT_Z,
        rotate
    ); //quat为围绕这个z轴旋转d度的四元数
    let rotmat3 = Cesium.Matrix3.fromQuaternion(quat); //rot_mat3为根据四元数求得的旋转矩阵
    let v = new Cesium.Cartesian3(trans.x, trans.y, trans.z); //p1的局部坐标
    let m = Cesium.Matrix4.fromRotationTranslation(
        rotmat3,
        Cesium.Cartesian3.ZERO
    ); //m为旋转加平移的4x4变换矩阵
    let m2 = [];
    Cesium.Matrix4.multiplyByTranslation(m, v, m2); //m = m X v
    let cart3 = position; //得到局部坐标原点的全局坐标
    let m1 = Cesium.Transforms.eastNorthUpToFixedFrame(cart3); //m1为局部坐标的z轴垂直于地表，局部坐标的y轴指向正北的4x4变换矩阵
    let m3 = [];
    Cesium.Matrix4.multiplyTransformation(m1, m2, m3); //m = m X m1
    let p2 = [];
    Cesium.Matrix4.getTranslation(m3, p2); //根据最终变换矩阵m得到p2
    return new Cesium.Cartesian3(p2.x, p2.y, p2.z);
}
